﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using DirectUI.Mobile.UI;
using System.Drawing.Imaging;
using DirectUI.Mobile.UI;
namespace DirectUI.Mobile.UI.Controls
{
  public class DirectUICheckBox:DirectUIControl
    {
      private Bitmap _imageOn;
      private Bitmap _imageOff;
      private ImageAttributes ia;
      public DirectUICheckBox()
      {
          _imageOff = Resource.cbnone;
          _imageOn = Resource.cbon;
          ia = new ImageAttributes();

          ia.SetColorKey(_imageOn.GetPixel(0, 0), _imageOn.GetPixel(0, 0));
          Height = 20;
          Width = 20;
      }
      private bool _checked = false;
      public bool IfChecked
      {
          get
          {
              return _checked;
          }
          set
          {
              if (_checked != value)
              {
                  if (value)
                  {
                      OnChecked(null);
                  }
                  else
                  {
                      OnUnChecked(null);
                  }
                  _checked = value;
                  OnPropertyChanged(new DirectUI.Mobile.UI.PropertyChangingEventArgs("IfChecked"));

                  this.Invalidate();
              }
          }
      }
      public override bool OnClick(PointEventArgs e)
      {
          IfChecked = !IfChecked;
          return base.OnClick(e);
      }
      public override void OnPaint(DirectUIPaintEventArgs e)
      {
          //base.OnPaint(e);
          if (IfChecked)
          {

         //     e.Graphics.DrawImage(_imageOn, e.ControlBounds, new Rectangle(0, 0, 20, 20), GraphicsUnit.Pixel);
              e.Graphics.DrawImage(_imageOn, e.ControlBounds, 0, 0, 20, 20, GraphicsUnit.Pixel, ia);
          }
          else
          {
            //  e.Graphics.DrawImage(_imageOff, e.ControlBounds, new Rectangle(0, 0, 20, 20), GraphicsUnit.Pixel);
              e.Graphics.DrawImage(_imageOff, e.ControlBounds, 0, 0, 20, 20, GraphicsUnit.Pixel, ia);

          }
      }
      public event EventHandler Checked;
      public event EventHandler UnChecked;
      public virtual void OnChecked(EventArgs e)
      {
          if (Checked != null)
          {
              Checked(this, e);
          }
      }
      public virtual void OnUnChecked(EventArgs e)
      {
          if (UnChecked != null)
          {
              UnChecked(this, e);
          }
      }

    }
}
